<template>
  <div>
    <!-- 兑奖列表 -->
    <my-breadcrumb :items="['衣柜管理', '种草单']"/>
    <search :formItems='searchItems' @search='getList' ref='search' />
    <el-button type="primary" class="margin10" @click="getExcel" :loading="loading" style="margin-bottom: 10px;">导出</el-button>
    <el-table :data='tableData' @sort-change="sortChange" style='width: 100%'>
      <el-table-column type="index" label='序号' ></el-table-column>
      <el-table-column prop='username' label='用户名称' ></el-table-column>
      <el-table-column prop='name' label='清单项'></el-table-column>
      <el-table-column prop='status' label='清单项状态' >
        <template slot-scope="scope">
          {{type[scope.row.status - 1]}}
        </template>
      </el-table-column>
      <el-table-column prop='names' label='所属裙子'></el-table-column>
      <el-table-column prop='businessname' label='所属商家'></el-table-column>
      <el-table-column prop='status' label='操作'>
        <template slot-scope="scope">
          <span class="astyle"  @click="editItem(scope.row)">编辑</span>
          <span class="astyle"  @click="deleteItem(1, scope.row.id)">删除</span>
          <!-- <span v-if="scope.row.status === '1'">完成发奖</span> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
        <!-- 表单start -->
    <el-dialog
      :close-on-click-modal="false"
      title="编辑"
      :visible.sync="showAddBox"
      >
      <el-form :model="diaForm" :rules="rules" ref="wardrobeListForm" label-width="110px">

        <el-form-item label="用户名称" prop="type">
          {{diaForm.username}}
        </el-form-item>

        <el-form-item label="清单项" prop="form1">
          {{diaForm.name}}
        </el-form-item>

        <el-form-item label="清单项状态" prop="form2">
          {{type[diaForm.status - 1]}}
        </el-form-item>

        <el-form-item label="所属裙子" prop="form3">
          <div class="select-box">
            <img :src="nowSkirt.img1" alt="" width="40" v-if="nowSkirt.img1">
            <span v-if="nowSkirt.name">{{ nowSkirt.name }}</span>
            <el-button type="text" @click="toSelectSkirt">点击选择裙子</el-button>
          </div>
        </el-form-item>

        <el-form-item label="所属商家" prop="form4">
          {{diaForm.businessname}}
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddBox = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSendForm('wardrobeListForm')">确 定</el-button>
      </span>
    </el-dialog>
    <select-list ref="selectList" :url="'/Skirti/index?status=0&delflag=0'" :searchItems="skirtSearch" :tableItem="skirtTableItems" @selectRow="skirtChange"/>
    <!-- 表单end -->
  </div>
</template>
<script>
import {toExport} from '@/utils/getList'
import tableItems from '@/config/tableItems'
import selectList from '@/components/common/selectList'
export default {
  name: 'grassList',
  data () {
    return {
      skirtSearch: [
        {
          label: '裙子名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '商家名称',
          type: 'input',
          key: 'businessname'
        }
      ], // 检索结构
      nowSkirt: {},
      skirtTableItems: tableItems.skirtList, // 选择裙子表格结构
      showAddBox: false,
      diaForm: {},
      loading: false,
      tableData: [],
      searchItems: [
        {
          label: '用户名称',
          type: 'input',
          key: 'username'
        },
        {
          label: '清单项',
          type: 'input',
          key: 'name'
        },
        {
          label: '清单项状态',
          type: 'select',
          key: 'status',
          width: '100',
          options: [{ id: '', name: '全部' }, { id: '1', name: '未勾选' }, { id: '2', name: '已入' }, { id: '3', name: '拔草' }]
        },
        {
          label: '所属裙子',
          type: 'input',
          key: 'names'
        },
        {
          label: '所属商家',
          type: 'input',
          key: 'businessname'
        }
      ],
      type: ['未勾选', '已入', '拔草'],
      rules: {},
      page: {
        page: 1,
        size: 20,
        total: 1
      },
      sortVal: {}
    }
  },
  methods: {
    toSelectSkirt () {
      this.$refs['selectList'].toShow()
    },
    skirtChange (row) {
      this.nowSkirt = row
      this.diaForm.htskirtid = row.id
      this.diaForm.businessname = row.membername
    },
    getList (page = 1) {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {
        ...searchData,
        page: page,
        size: this.page.size
      }
      if (typeof page !== 'number') {
        params.page = 1
      }
      if (this.sortVal.prop) {
        params.orderby = this.sortVal.prop + ' ' + this.sortVal.order
      }
      this.axios.get('/clothes/weed_list', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          this.page.page = page
          this.page.total = Number(res.data.data.count)
          this.tableData = res.data.data.list
        }
      })
    },
    deleteItem (e = 1, val) {
      let self = this
      let params = {}
      params.id = val
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/clothes/del_weed', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            self.$message({
              message: '删除成功',
              type: 'success'
            })
            self.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList(val)
    },
    getExcel () {
      this.loading = true
      let searchData = this.$refs['search'].search()
      toExport('/clothes/excel_weed', searchData, '种草单列表', () => {
        this.loading = false
      })
    },
    sortChange (data) {
      this.sortVal.prop = data.prop
      if (data.order === 'ascending') {
        this.sortVal.order = 'asc'
      } else if (data.order === 'descending') {
        this.sortVal.order = 'desc'
      } else {
        this.sortVal.order = ''
      }
      this.getList()
    },
    editItem (data) {
      this.showAddBox = true
      this.diaForm = data
    },
    handleClickSendForm (formName) {
      let self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            id: self.diaForm['id'],
            htskirtid: self.diaForm['htskirtid']
          }
          this.axios.post('/clothes/edit_weed', self.qs.stringify(params)).then(res => {
            if (res.data.status === 1 || res.data.status === '1') {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.showAddBox = false
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
    this.getList()
  },
  components: {
    selectList
  }
}
</script>
<style scoped>
 .astyle{
    color: #409EFF;
    cursor:pointer;
 }
 .astyle:hover{
   color: #66b1ff;
 }
</style>
